package br.com.sigarota.facade;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;

import br.com.sigarota.dao.PontoDAO;
import br.com.sigarota.modelo.Ponto;

public class PontoFacadeImpl implements PontoFacade {

	private static final long serialVersionUID = 1818242808424001885L;

	private PontoDAO PontoDAO;
	private SessionFactory sf; 
	private	Session session;
	private Transaction tx;

	public void salva(Ponto p) {
		sf = new AnnotationConfiguration().configure().buildSessionFactory(); 
		session = sf.openSession();
		tx = session.beginTransaction();
		PontoDAO = new PontoDAO(session, Ponto.class);
		this.PontoDAO.save(p);
		tx.commit();
		session.close();
	}

	public void atualiza(Ponto p) {
		this.sf = new AnnotationConfiguration().configure().buildSessionFactory(); 
		this.session = sf.openSession();
		this.tx = session.beginTransaction();
		this.PontoDAO = new PontoDAO(session, Ponto.class);
		this.PontoDAO.merge(p);
		this.tx.commit();
		this.session.close();
	}

	public Ponto procura(Long id) {
		sf = new AnnotationConfiguration().configure().buildSessionFactory(); 
		session = sf.openSession();
		tx = session.beginTransaction();
		PontoDAO = new PontoDAO(session, Ponto.class);
		Ponto p = this.PontoDAO.load(id);
		tx.commit();
		session.close();
		return p;
	}

	public void remove(Ponto p) {
		sf = new AnnotationConfiguration().configure().buildSessionFactory(); 
		session = sf.openSession();
		tx = session.beginTransaction();
		PontoDAO = new PontoDAO(session, Ponto.class);
		this.PontoDAO.delete(p);
		tx.commit();
		session.close();
	}

	public List<Ponto> lista() {
		this.sf = new AnnotationConfiguration().configure().buildSessionFactory(); 
		this.session = sf.openSession();
		this.tx = session.beginTransaction();
		this.PontoDAO = new PontoDAO(this.session, Ponto.class);
		List<Ponto> lista = this.PontoDAO.list();
		this.tx.commit();
		this.session.close();
		return lista;
	}

	public List<Ponto> pesquisaPontosByNome(String nome) {
		sf = new AnnotationConfiguration().configure().buildSessionFactory(); 
		session = sf.openSession();
		tx = session.beginTransaction();
		PontoDAO = new PontoDAO(session, Ponto.class);
		List<Ponto> lista = this.PontoDAO.pesquisaPontos(nome);
		tx.commit();
		session.close();
		return lista;
	}

	public Ponto procuraById(Long id){
		sf = new AnnotationConfiguration().configure().buildSessionFactory(); 
		session = sf.openSession();
		tx = session.beginTransaction();
		PontoDAO = new PontoDAO(session, Ponto.class);
		Ponto p = this.PontoDAO.pesquisaPontoById(id);
		tx.commit();
		session.close();
		return p;
	}

	public Ponto procuraByNome(String nome){
		sf = new AnnotationConfiguration().configure().buildSessionFactory(); 
		session = sf.openSession();
		tx = session.beginTransaction();
		PontoDAO = new PontoDAO(session, Ponto.class);
		Ponto p = this.PontoDAO.pesquisaPontoByNome(nome);
		tx.commit();
		session.close();
		return p; 
	}
}